package com.notetool.util.tokenparser;

import com.notetool.util.tokenparser.syntax.SingleCharModel;

public class SingleCharToken extends Token
{
    
    ////////////////////
    // Members
    private SingleCharModel singleCharModel;
    private int count;
    
    ////////////////////
    // Constructors
    public SingleCharToken (SingleCharModel scModel)
    {
        super (scModel);
        
        singleCharModel = scModel;
        count = 1;
    }
    
    ////////////////////
    // Token implementation
    public String toString ()
    { 
        if (singleCharModel == null)
            return null;
        
        // Repeat char n times where n is the number of sticky separator tokens
        StringBuilder builder = new StringBuilder ();
        for (int i = 0; i < count; ++ i)
            builder.append (singleCharModel.getCharacter ());
        
        return builder.toString ();
    }
    
    public Token link (Token token)
    {     
        if (token == null)
            return null;
        
        // Check if we're linking to a same type token
        if (singleCharModel == token.getModel ())
        {
            // If we don't add the token because of specific rules, we return this token... it's the element representing the token we just added    
            if (singleCharModel.isUnique ())
                return this;
            
            if (singleCharModel.isGrouped ())
            {
                count += 1;
                return this;
            }
        }
        
        // If we make it here, we're just linking like a normal Token
        return super.link (token);
    }
    
    ////////////////////
    // Private methods
    
}
